package com.ysx.member.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysx.common.context.LoginMemberContext;
import com.ysx.common.repentity.PageResp;
import com.ysx.common.util.SnowUtil;
import com.ysx.member.domain.Passenger;
import com.ysx.member.domain.PassengerExample;
import com.ysx.member.entity.PassengerQueryReq;
import com.ysx.member.entity.PassengerSaveReq;
import com.ysx.member.mapper.PassengerMapper;
import com.ysx.member.repentity.PassengerQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 乘客信息服务类
 * 提供乘客信息的保存和查询功能
 */
@Service
public class PassengerService {
    private static final Logger LOG = LoggerFactory.getLogger(PassengerService.class);


    //插入
    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 保存乘客信息
     * 如果乘客ID为空，则新增乘客信息；否则更新已有乘客信息
     * @param req 乘客信息保存请求参数
     */
    public void save(PassengerSaveReq req) {
        Date date = new Date();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
        // 判断是新增还是更新操作
        if (ObjectUtil.isNull(req.getId())){
            passenger.setMemberId(LoginMemberContext.getId());//获取当前登录用户的id

            passenger.setId(SnowUtil.getSnowflakeNextId());//自己封装的雪花算法id
            passenger.setCreateTime(date);
            passenger.setUpdateTime(date);
            passengerMapper.insert(passenger);
        } else {
            passenger.setUpdateTime(date);
            passengerMapper.updateByPrimaryKey(passenger);

        }


    }

    /**
     * 查询乘客信息列表
     * @param req 乘客信息查询请求参数
     * @return 分页返回乘客信息列表
     */
    //查询
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        // 根据会员ID过滤乘客信息
        if (ObjectUtil.isNotNull( req.getMemberId())){

            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        
        PageHelper.startPage(req.getPage(), req.getSize());

        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<PassengerQueryResp> list = BeanUtil.copyToList(passengers, PassengerQueryResp.class);
        PageResp<PassengerQueryResp> PageResp = new PageResp<>();
        PageResp.setTotal(pageInfo.getTotal());
        PageResp.setList( list);
        return PageResp;
    }

    public void delete(Long id)
    {
        passengerMapper.deleteByPrimaryKey(id);
    }

}